package ym.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ym.pojo.House;
import ym.service.CollectionService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 收藏
 */
@CrossOrigin
@RestController
@RequestMapping("/front/collection")
public class FrontCollectionCon {
    @DubboReference
    private CollectionService collectionService;

    /**
     * 收藏
     *
     * @param userId
     * @param houseId
     * @return
     */
    @PostMapping("/add")
    public BaseResult add(Integer userId, Integer houseId) {
        return collectionService.add(userId, houseId);
    }

    /**
     * 取消收藏
     *
     * @param userId
     * @param houseId
     * @return
     */
    @PostMapping("/delete")
    public BaseResult delete(Integer userId, Integer houseId) {
        collectionService.delete(userId, houseId);
        return BaseResult.ok();
    }

    /**
     * 根据用户id查询所有收藏的房源
     *
     * @param userId
     * @return
     */
    @GetMapping("/findAllByUserId")
    public BaseResult findAllByUserId(Integer userId) {
        List<House> houseList = collectionService.findAllByUserId(userId);
        return BaseResult.ok(houseList);
    }

    /**
     * 判断是否收藏
     *
     * @param userId
     * @param houseId
     * @return
     */
    @GetMapping("/isCollect")
    public BaseResult isCollect(Integer userId, Integer houseId) {
        Boolean isCollect = collectionService.isCollect(userId, houseId);
        return BaseResult.ok(isCollect);
    }
}
